package com.example.pkmedical.Activity.servicecode;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SerialProtUtil;
import com.example.pkmedical.Utils.ToastUtils;
import com.example.pkmedical.View.InputFilterMinMax;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BedCalibrationActivity extends BaseActivity {

    private static final String TAG = "yangshuo";
    @BindView(R.id.top_bar)
    TextView topBar;
    @BindView(R.id.calibration_bed)
    ImageView calibrationBed;
    @BindView(R.id.calibration_edit)
    EditText calibrationEdit;
    @BindView(R.id.calibration_layout)
    LinearLayout calibrationLayout;
    @BindView(R.id.calibration_ok)
    Button calibrationOk;
    @BindView(R.id.calibration_cancle)
    Button calibrationCancle;
    @BindView(R.id.input_zero_layout)
    RelativeLayout inputZeroLayout;
    @BindView(R.id.input_fama_layout)
    RelativeLayout inputFamaLayout;
    private byte[] mBytes;
    private boolean isToZero = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calibration);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        calibrationEdit.setFilters(new InputFilter[]{new InputFilterMinMax("1", "100")});
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.CALIBRATION_WEIGHT) {
            Log.d(TAG, "onDataReceivedCalibration [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceivedCalibration [ byte[] ]: NoThisData");
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {


        if (DistinguishUtil.distinguishUtil(bytes)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[3]) {
                        case 0x01:
                            toZero();
                            break;
                        case 0x02:
                            doNext();
                            break;
                    }
                }
            });
        } else {
        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.calibration_ok, R.id.calibration_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.calibration_ok:
                if (isToZero) {
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CALIBRATION_TOZERO);
                } else {
                    if (!TextUtils.isEmpty(calibrationEdit.getText().toString())){
                        int mData = Integer.parseInt(calibrationEdit.getText().toString());
                        if (!TextUtils.isEmpty(mData + "") && mData >= 5) {
                            byte[] mCalibationBytes = CommandUtil.CALIBRATION_WEIGHT;
                            mCalibationBytes[6] = Byte.parseByte(calibrationEdit.getText().toString());
                            SerialProtUtil.getInstance().sendBytes(2, mCalibationBytes);
                        } else {
                            ToastUtils.show(MyApp.getAppContext(), getResources().getString(R.string.null_number_area));
                        }
                    }else {
                        ToastUtils.show(MyApp.getAppContext(),"不能为空");
                    }

                }

                break;
            case R.id.calibration_cancle:
                BedCalibrationActivity.this.finish();
                break;
        }
    }

    private void toZero() {
        isToZero = true;
        calibrationBed.setImageResource(R.drawable.img_input_tozero);
        inputFamaLayout.setVisibility(View.GONE);
        inputZeroLayout.setVisibility(View.VISIBLE);
    }

    private void doNext() {
        startActivity(new Intent(MyApp.getAppContext(), CalibrationSensorActivity.class));
        BedCalibrationActivity.this.finish();
//        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
}
